package _04sql.work1;
import com.mysql.cj.jdbc.Driver;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AccountDaoImpl implements AccountDao{
    public static Connection getConnection() throws Exception {
        Connection connection = null;
        DriverManager.registerDriver(new Driver());
        String url = "jdbc:mysql://localhost:3306/db?useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8";
        String username = "root";
        String password = "123456";
        connection = DriverManager.getConnection(url, username, password);
        return connection;
    }
    public static void  close(Connection connection, Statement statement, ResultSet resultSet){
        try {
            if (resultSet != null) resultSet.close();
            if (statement!= null) statement.close();
            if (connection != null) connection.close();

        }catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    @Override
    public BigDecimal selectMoneyByName(String name) {
        try {
            Connection connection = getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from acount");
            List<Count> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String names = resultSet.getString("name");
                double money = resultSet.getDouble("money");
                BigDecimal bigDecimalmoney = BigDecimal.valueOf(money);
                Count count = new Count(id, names, bigDecimalmoney);
                list.add(count);
            }
            Count[] counts = list.toArray(new Count[0]);
            BigDecimal bigDecimal = null;
            for (Count count : counts) {
                if (count.getName().equals(name)) {
                    bigDecimal = count.getMoney();
                    break;
                }
            }
            close(connection,statement,resultSet);
            return bigDecimal;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateMoneyByName(String name, BigDecimal money) {
        try {
            Connection connection = getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet=statement.executeQuery("select * from acount");
            List<Count> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String names = resultSet.getString("name");
                double money1 = resultSet.getDouble("money");
                BigDecimal bigDecimalmoney = BigDecimal.valueOf(money1);
                Count count = new Count(id, names, bigDecimalmoney);
                list.add(count);
            }
            Count[] counts = list.toArray(new Count[0]);
            double d=0;
            for (Count count : counts) {
                if (count.getName().equals(name)) {
                    d+=count.getMoney().doubleValue();
                    break;
                }
            }
            d+=money.doubleValue();
            System.out.println(d);
            int affectedRows1 = statement.executeUpdate("update acount set money=0 where name='张三'");
            int affectedRows = statement.executeUpdate("update acount set money="+d+" where name='"+name+"'");
            System.out.println(affectedRows);
            close(connection,statement,resultSet);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static void main(String[] args) throws Exception {
        AccountDaoImpl accountDao = new AccountDaoImpl();
        BigDecimal bigDecimal=accountDao.selectMoneyByName("张三");
        System.out.println(bigDecimal);
        if (accountDao.updateMoneyByName("李四",bigDecimal)==1){
            System.out.println("转账成功");
        }else System.out.println("转账失败");

    }
}
class Count{
    private int id;
    private String name;
    private BigDecimal money;

    public Count(int id, String name, BigDecimal money) {
        this.id = id;
        this.name = name;
        this.money = money;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Count{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
